//package com.plian.system.service.dp.impl;
//
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.entity.dp.DutyOrganizations;
//import com.plian.system.mapper.dp.DutyOrganizationsMapper;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.dp.IDutyOrganizationsService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.vo.dp.DutyOrganizationsVO;
//import com.plian.system.wrapper.dp.DutyOrganizationsWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// * 责任追究 追责机构 服务实现类
// *
// * @author gangan
// * @since 2021-05-08
// */
//@Slf4j
//@Service
//@AllArgsConstructor
//public class DutyOrganizationsServiceImpl extends BaseServiceImpl<DutyOrganizationsMapper, DutyOrganizations> implements IDutyOrganizationsService, BaseFlowService {
//
//    private FormWorkflowService formWorkflowService;
//
//    private IFormCodeService formCodeService;
//
//    private DutyOrganizationsWrapper dutyOrganizationsWrapper;
//
//    @Override
//    @Transactional(rollbackFor=RuntimeException.class)
//    public boolean saveOrUpdate(DutyOrganizations dutyOrganizations) {
//        if (dutyOrganizations.getId() == null){
//            dutyOrganizations.setCode(formCodeService.getCode(FormTypeConstant.DP_DUTY_ORGANIZATIONS_FORM));
//            dutyOrganizations.setFormType(String.valueOf(FormTypeConstant.DP_DUTY_ORGANIZATIONS_FORM));
//        }
//        boolean result = super.saveOrUpdate(dutyOrganizations);
//        if (dutyOrganizations.getStatus() == FormStatusConstant.UNREVIEWED){
//            HashMap<String, Object> processVariable = new HashMap<>();
//            formWorkflowService.startProcessAndSimulate(String.valueOf(dutyOrganizations.getId()),dutyOrganizations.getFormType(),processVariable);
//        }
//
//        return result;
//    }
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//        for (Long id : ids) {
//            DutyOrganizations dutyOrganizations = getById(id);
//            if (dutyOrganizations == null) {
//                return false;
//            } else if (dutyOrganizations.getStatus() != null &&
//                    (dutyOrganizations.getStatus() != FormStatusConstant.STASH && dutyOrganizations.getStatus() != FormStatusConstant.UNREVIEWED)) {
//                return false;
//            }
//            formWorkflowService.deleteProcessInstance(String.valueOf(id));
//        }
//        return super.deleteLogic(ids);
//    }
//
//    @Override
//    public HashMap<String,Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String,Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//        /**
//         * 设置审批人和审批时间
//         */
//        DutyOrganizations dutyOrganizations = getById(id);
//        dutyOrganizations.setApprovedUser(TokenUtil.getTokenUserId());
//        Date now = DateUtil.now();
//        dutyOrganizations.setApprovedTime(now);
//        updateById(dutyOrganizations);
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//
//    /**
//     * 统一处理状态机事件方法
//     * @param id 主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            DutyOrganizations dutyOrganizations = getById(id);
//            dutyOrganizations.setStatus(updateStatus);
//            updateById(dutyOrganizations);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//
//
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        QueryWrapper<DutyOrganizations> queryWrapper = new QueryWrapper<>();
//        queryWrapper.in("id", Func.toLongList(StringUtils.join(formIds, StringPool.COMMA)));
//        List<DutyOrganizations> dutyOrganizations = list(queryWrapper);
//        List<DutyOrganizationsVO> dutyOrganizationsVOS = dutyOrganizationsWrapper.entityToVO(dutyOrganizations);
//        List<PageData> pageDataList = new ArrayList<>();
//        try{
//            if (CollectionUtil.isNotEmpty(dutyOrganizationsVOS)){
//                for (DutyOrganizationsVO vo : dutyOrganizationsVOS){
//                    PageData pd = new PageData();
//                    pd.put("id", vo.getId());
//                    pd.put("UNITNAME", vo.getCompanyName());
//                    pd.put("APPLICANTNAME", vo.getCreateUserName());
//                    pd.put("applicantId", vo.getCreateUser());
//                    pd.put("applyOrganizationId", vo.getCompanyNameId());
//                    pd.put("formType", FormTypeConstant.DP_DUTY_ORGANIZATIONS_FORM);
//                    pd.put("orgName", vo.getCompanyName());
//                    pageDataList.add(pd);
//                }
//            }
//
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Arrays.asList(FormTypeConstant.DP_DUTY_ORGANIZATIONS_FORM);
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//
//    @Override
//    public Boolean checkIsExisted(Long id, String companyId) {
//        if (StringUtil.isNotBlank(companyId)) {
//            List<DutyOrganizations> list = list(new LambdaQueryWrapper<DutyOrganizations>().eq(DutyOrganizations::getCompanyNameId, companyId));
//            if (list.size()>0 ) {
//                if (id == null || !id.equals(list.get(0).getId())){
//                    return false;
//                }
//            }
//        }
//        return true;
//    }
//}
